{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71c38cfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imagenet_labels import ind2name\n",
    "import os\n",
    "from collections import defaultdict\n",
    "\n",
    "from PIL import Image\n",
    "from scipy.io import loadmat\n",
    "import numpy as np\n",
    "import random\n",
    "import shutil\n",
    "import torch \n",
    "import csv \n",
    "import json    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cedd3468",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# mini-ImageNet few-shot\n",
    "nway = 5 \n",
    "kshot = 1\n",
    "nquery = 16\n",
    "\n",
    "mini_imagenet_dir = 'miniImagenet/novel.json'\n",
    "\n",
    "with open(mini_imagenet_dir) as f:\n",
    "    mini_imagenet = json.load(f)\n",
    "\n",
    "mini_imagenet_imgs = mini_imagenet['image_names']\n",
    "code2imgs = {}\n",
    "    \n",
    "\n",
    "for img in mini_imagenet_imgs: \n",
    "    c = img.split(\"/\")[-2]\n",
    "    if c not in code2imgs: \n",
    "        code2imgs[c] = []\n",
    "    code2imgs[c].append(img)\n",
    "\n",
    "mini_imagenet_codes = [k for k in code2imgs.keys()]\n",
    "\n",
    "imagenet_dir = '/datasets/imagenet/train/'\n",
    "output_dir = './datasets/mini_imagenet_fs/'\n",
    " \n",
    "output_dir_train = os.path.join(output_dir, '{}way_{}shot'.format(nway, kshot))\n",
    "os.makedirs(output_dir_train, exist_ok = True) \n",
    "\n",
    "\n",
    "cls_code = os.listdir(imagenet_dir)\n",
    "cls_code = np.sort(cls_code)\n",
    "code2name = {}\n",
    "for i,code in enumerate(cls_code):\n",
    "    code2name[code] = ind2name[i]\n",
    "    \n",
    "    \n",
    "num_classes = len(mini_imagenet_codes)\n",
    "\n",
    "trials = 600 \n",
    "\n",
    "for seed in range(trials):\n",
    "    random.seed(seed)\n",
    "    np.random.seed(seed)\n",
    "    torch.manual_seed(seed)\n",
    "    torch.cuda.manual_seed(seed)\n",
    "    \n",
    "    dataset_path = os.path.join(output_dir_train, 'dataset{}'.format(seed))\n",
    "    os.makedirs(dataset_path, exist_ok = True) \n",
    "    \n",
    "    print('creating dataset {}'.format(seed))\n",
    "    \n",
    "    selected_classes = torch.randperm(num_classes)[:nway].numpy()\n",
    "    np.savetxt(os.path.join(dataset_path, 'cls_list.csv'), selected_classes, fmt='%s', delimiter=',')\n",
    "    \n",
    "    \n",
    "    classes = [mini_imagenet_codes[i] for i in selected_classes]\n",
    "    \n",
    "    \n",
    "    for code in classes: \n",
    "        cls_name = code2name[code]\n",
    "        print(\"sampling class {}\".format(cls_name))\n",
    "        \n",
    "        train_dst_cls_dir = os.path.join(dataset_path, 'train/{}_{}'.format(code, cls_name))\n",
    "        val_dst_cls_dir = os.path.join(dataset_path, 'val/{}_{}'.format(code, cls_name))\n",
    "        os.makedirs(train_dst_cls_dir, exist_ok = True) \n",
    "        os.makedirs(val_dst_cls_dir, exist_ok = True) \n",
    "        \n",
    "        imgs = code2imgs[code]\n",
    "        selected_train_images_inds = torch.randperm(len(imgs))[:kshot].numpy()\n",
    "        selected_val_images_inds = torch.randperm(len(imgs))[kshot:kshot+nquery].numpy()\n",
    "        selected_images_train = [imgs[i] for i in selected_train_images_inds]\n",
    "        selected_images_val = [imgs[i] for i in selected_val_images_inds]\n",
    "        \n",
    "        for src_img in selected_images_train: \n",
    "            img_file = src_img.split(\"/\")[-1]\n",
    "            dst_img_path = os.path.join(train_dst_cls_dir, img_file)\n",
    "            shutil.copyfile(src_img, dst_img_path)\n",
    "        \n",
    "        for src_img in selected_images_val: \n",
    "            img_file = src_img.split(\"/\")[-1]\n",
    "            dst_img_path = os.path.join(val_dst_cls_dir, img_file)\n",
    "            shutil.copyfile(src_img, dst_img_path)\n",
    "            \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b24da78",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "\n",
    "# mini-ImageNet few-shot\n",
    "nway = 5 \n",
    "kshot = 5\n",
    "nquery = 16\n",
    "\n",
    "mini_imagenet_dir = 'miniImagenet/novel.json'\n",
    "\n",
    "with open(mini_imagenet_dir) as f:\n",
    "    mini_imagenet = json.load(f)\n",
    "\n",
    "mini_imagenet_imgs = mini_imagenet['image_names']\n",
    "code2imgs = {}\n",
    "    \n",
    "\n",
    "for img in mini_imagenet_imgs: \n",
    "    c = img.split(\"/\")[-2]\n",
    "    if c not in code2imgs: \n",
    "        code2imgs[c] = []\n",
    "    code2imgs[c].append(img)\n",
    "\n",
    "mini_imagenet_codes = [k for k in code2imgs.keys()]\n",
    "\n",
    "imagenet_dir = '/datasets/imagenet/train/'\n",
    "output_dir = './datasets/mini_imagenet_fs/'\n",
    " \n",
    "output_dir_train = os.path.join(output_dir, '{}way_{}shot'.format(nway, kshot))\n",
    "os.makedirs(output_dir_train, exist_ok = True) \n",
    "\n",
    "\n",
    "cls_code = os.listdir(imagenet_dir)\n",
    "cls_code = np.sort(cls_code)\n",
    "code2name = {}\n",
    "for i,code in enumerate(cls_code):\n",
    "    code2name[code] = ind2name[i]\n",
    "    \n",
    "    \n",
    "num_classes = len(mini_imagenet_codes)\n",
    "\n",
    "trials = 600 \n",
    "\n",
    "for seed in range(trials):\n",
    "    random.seed(seed)\n",
    "    np.random.seed(seed)\n",
    "    torch.manual_seed(seed)\n",
    "    torch.cuda.manual_seed(seed)\n",
    "    \n",
    "    dataset_path = os.path.join(output_dir_train, 'dataset{}'.format(seed))\n",
    "    os.makedirs(dataset_path, exist_ok = True) \n",
    "    \n",
    "    print('creating dataset {}'.format(seed))\n",
    "    \n",
    "    selected_classes = torch.randperm(num_classes)[:nway].numpy()\n",
    "    np.savetxt(os.path.join(dataset_path, 'cls_list.csv'), selected_classes, fmt='%s', delimiter=',')\n",
    "    \n",
    "    \n",
    "    classes = [mini_imagenet_codes[i] for i in selected_classes]\n",
    "    \n",
    "    \n",
    "    for code in classes: \n",
    "        cls_name = code2name[code]\n",
    "        print(\"sampling class {}\".format(cls_name))\n",
    "        \n",
    "        train_dst_cls_dir = os.path.join(dataset_path, 'train/{}_{}'.format(code, cls_name))\n",
    "        val_dst_cls_dir = os.path.join(dataset_path, 'val/{}_{}'.format(code, cls_name))\n",
    "        os.makedirs(train_dst_cls_dir, exist_ok = True) \n",
    "        os.makedirs(val_dst_cls_dir, exist_ok = True) \n",
    "        \n",
    "        imgs = code2imgs[code]\n",
    "        selected_train_images_inds = torch.randperm(len(imgs))[:kshot].numpy()\n",
    "        selected_val_images_inds = torch.randperm(len(imgs))[kshot:kshot+nquery].numpy()\n",
    "        selected_images_train = [imgs[i] for i in selected_train_images_inds]\n",
    "        selected_images_val = [imgs[i] for i in selected_val_images_inds]\n",
    "        \n",
    "        for src_img in selected_images_train: \n",
    "            img_file = src_img.split(\"/\")[-1]\n",
    "            dst_img_path = os.path.join(train_dst_cls_dir, img_file)\n",
    "            shutil.copyfile(src_img, dst_img_path)\n",
    "        \n",
    "        for src_img in selected_images_val: \n",
    "            img_file = src_img.split(\"/\")[-1]\n",
    "            dst_img_path = os.path.join(val_dst_cls_dir, img_file)\n",
    "            shutil.copyfile(src_img, dst_img_path)\n",
    "            \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53515d5c",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f2133d8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "\n",
    "# tiered-ImageNet few-shot\n",
    "nway = 5 \n",
    "kshot = 1\n",
    "nquery = 16\n",
    "\n",
    "imagenet_tiered_dir = '/tiered-imagenet-tools/tiered_imagenet/test/'\n",
    "output_dir = './datasets/tiered_imagenet_fs/'\n",
    "words_dir = '/tiered-imagenet-tools/tiered_imagenet_split/words.txt'\n",
    "output_dir_train = os.path.join(output_dir, '{}way_{}shot'.format(nway, kshot))\n",
    "os.makedirs(output_dir_train, exist_ok = True) \n",
    "\n",
    "\n",
    "file1 = open(words_dir, 'r')\n",
    "Lines = file1.readlines()\n",
    "\n",
    "code2name = {}\n",
    "for l in Lines: \n",
    "    c = l[:9]\n",
    "    n = l[10:-1]\n",
    "    code2name[c] = n\n",
    "    \n",
    "\n",
    "cls_code = os.listdir(imagenet_tiered_dir)\n",
    "cls_code = np.sort(cls_code)\n",
    "num_classes = len(cls_code)\n",
    "    \n",
    "\n",
    "trials = 1000\n",
    "\n",
    "for seed in range(trials):\n",
    "    random.seed(seed)\n",
    "    np.random.seed(seed)\n",
    "    torch.manual_seed(seed)\n",
    "    torch.cuda.manual_seed(seed)\n",
    "    \n",
    "    dataset_path = os.path.join(output_dir_train, 'dataset{}'.format(seed))\n",
    "    os.makedirs(dataset_path, exist_ok = True) \n",
    "    \n",
    "    print('creating dataset {}'.format(seed))\n",
    "    \n",
    "    selected_classes = torch.randperm(num_classes)[:nway].numpy()\n",
    "    np.savetxt(os.path.join(dataset_path, 'cls_list.csv'), selected_classes, fmt='%s', delimiter=',')\n",
    "    \n",
    "    classes = [cls_code[i] for i in selected_classes]\n",
    "    \n",
    "    for code in classes: \n",
    "        cls_name = code2name[code]\n",
    "        print(\"sampling class {}\".format(cls_name))\n",
    "        src_cls_dir = os.path.join(imagenet_tiered_dir, code)        \n",
    "        train_dst_cls_dir = os.path.join(dataset_path, 'train/{}_{}'.format(code, cls_name))\n",
    "        val_dst_cls_dir = os.path.join(dataset_path, 'val/{}_{}'.format(code, cls_name))\n",
    "        os.makedirs(train_dst_cls_dir, exist_ok = True) \n",
    "        os.makedirs(val_dst_cls_dir, exist_ok = True) \n",
    "        \n",
    "        imgs = os.listdir(src_cls_dir)\n",
    "        \n",
    "        selected_train_images_inds = torch.randperm(len(imgs))[:kshot].numpy()\n",
    "        selected_val_images_inds = torch.randperm(len(imgs))[kshot:kshot+nquery].numpy()\n",
    "        selected_images_train = [imgs[i] for i in selected_train_images_inds]\n",
    "        selected_images_val = [imgs[i] for i in selected_val_images_inds]\n",
    "        \n",
    "        for src_img in selected_images_train: \n",
    "            dst_img_path = os.path.join(train_dst_cls_dir, src_img)\n",
    "            src_img_path = os.path.join(src_cls_dir, src_img)\n",
    "            shutil.copyfile(src_img_path, dst_img_path)\n",
    "        \n",
    "        for src_img in selected_images_val: \n",
    "\n",
    "            dst_img_path = os.path.join(val_dst_cls_dir, src_img)\n",
    "            src_img_path = os.path.join(src_cls_dir, src_img)\n",
    "            shutil.copyfile(src_img_path, dst_img_path)\n",
    "            \n",
    "            \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdecbe13",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# tiered-ImageNet few-shot\n",
    "nway = 5 \n",
    "kshot = 5\n",
    "nquery = 16\n",
    "\n",
    "imagenet_tiered_dir = '/tiered-imagenet-tools/tiered_imagenet/test/'\n",
    "output_dir = './datasets/tiered_imagenet_fs/'\n",
    "words_dir = '/tiered-imagenet-tools/tiered_imagenet_split/words.txt'\n",
    "output_dir_train = os.path.join(output_dir, '{}way_{}shot'.format(nway, kshot))\n",
    "os.makedirs(output_dir_train, exist_ok = True) \n",
    "\n",
    "\n",
    "file1 = open(words_dir, 'r')\n",
    "Lines = file1.readlines()\n",
    "\n",
    "code2name = {}\n",
    "for l in Lines: \n",
    "    c = l[:9]\n",
    "    n = l[10:-1]\n",
    "    code2name[c] = n\n",
    "    \n",
    "\n",
    "cls_code = os.listdir(imagenet_tiered_dir)\n",
    "cls_code = np.sort(cls_code)\n",
    "num_classes = len(cls_code)\n",
    "    \n",
    "\n",
    "trials = 1000\n",
    "\n",
    "for seed in range(trials):\n",
    "    random.seed(seed)\n",
    "    np.random.seed(seed)\n",
    "    torch.manual_seed(seed)\n",
    "    torch.cuda.manual_seed(seed)\n",
    "    \n",
    "    dataset_path = os.path.join(output_dir_train, 'dataset{}'.format(seed))\n",
    "    os.makedirs(dataset_path, exist_ok = True) \n",
    "    \n",
    "    print('creating dataset {}'.format(seed))\n",
    "    \n",
    "    selected_classes = torch.randperm(num_classes)[:nway].numpy()\n",
    "    np.savetxt(os.path.join(dataset_path, 'cls_list.csv'), selected_classes, fmt='%s', delimiter=',')\n",
    "    \n",
    "    classes = [cls_code[i] for i in selected_classes]\n",
    "    \n",
    "    for code in classes: \n",
    "        cls_name = code2name[code]\n",
    "        print(\"sampling class {}\".format(cls_name))\n",
    "        src_cls_dir = os.path.join(imagenet_tiered_dir, code)        \n",
    "        train_dst_cls_dir = os.path.join(dataset_path, 'train/{}_{}'.format(code, cls_name))\n",
    "        val_dst_cls_dir = os.path.join(dataset_path, 'val/{}_{}'.format(code, cls_name))\n",
    "        os.makedirs(train_dst_cls_dir, exist_ok = True) \n",
    "        os.makedirs(val_dst_cls_dir, exist_ok = True) \n",
    "        \n",
    "        imgs = os.listdir(src_cls_dir)\n",
    "        \n",
    "        selected_train_images_inds = torch.randperm(len(imgs))[:kshot].numpy()\n",
    "        selected_val_images_inds = torch.randperm(len(imgs))[kshot:kshot+nquery].numpy()\n",
    "        selected_images_train = [imgs[i] for i in selected_train_images_inds]\n",
    "        selected_images_val = [imgs[i] for i in selected_val_images_inds]\n",
    "        \n",
    "        for src_img in selected_images_train: \n",
    "            dst_img_path = os.path.join(train_dst_cls_dir, src_img)\n",
    "            src_img_path = os.path.join(src_cls_dir, src_img)\n",
    "            shutil.copyfile(src_img_path, dst_img_path)\n",
    "        \n",
    "        for src_img in selected_images_val: \n",
    "\n",
    "            dst_img_path = os.path.join(val_dst_cls_dir, src_img)\n",
    "            src_img_path = os.path.join(src_cls_dir, src_img)\n",
    "            shutil.copyfile(src_img_path, dst_img_path)\n",
    "            \n",
    "            \n",
    "    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
